<template>
  <div class="app-container">
    <div class="server">

      <!-- 搜索框和按钮 -->
      <el-form ref="searchForm" :model="searchForm" :inline="true" class="top-form">
        <el-form-item>
          <el-select v-model="searchForm.idc" style="width: 150px">
            <el-option v-for="(item, index) in searchIdcsList"
              :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.cabinet" style="width: 150px">
            <el-option v-for="(item, index) in searchCabinetList"
              :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.business_parent" style="width: 150px">
            <el-option v-for="(item, index) in searchBusinessList"
              :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.business_line" style="width: 150px">
            <el-option v-for="(item, index) in searchBusinessLineList"
              :key="index" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchForm.hostname" placeholder="请输入主机名或IP地址"
                    @keyup.enter.native="searchClick" style="width: 180px"></el-input>
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="searchClick()">搜索</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据列表 -->
      <el-table :data="serverList" :v-loading="isLoading" element-loading-text="拼命加载中" border style="width: 100%">
        <!-- 详细信息 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand" label-width="90px">
              <el-form-item label="主机名">
                <span>{{ props.row.hostname }}</span>
              </el-form-item>
              <el-form-item label="IP">
                <span>{{ props.row.ip }}</span>
              </el-form-item>
              <el-form-item label="操作系统">
                <span>{{ props.row.os }}</span>
              </el-form-item>
              <el-form-item label="CPU">
                <span>{{ props.row.cpu }}</span>
              </el-form-item>
              <el-form-item label="内存">
                <span>{{ props.row.mem }}</span>
              </el-form-item>
              <el-form-item label="磁盘">
                <span>{{ props.row.disk }}</span>
              </el-form-item>
              <el-form-item label="所属机房">
                <span>{{ props.row.idc.name }}</span>
              </el-form-item>
              <el-form-item label="所属机柜">
                <span>{{ props.row.cabinet.name }}</span>
              </el-form-item>
              <el-form-item label="厂商">
                <span>{{ props.row.manufacturer.vendor_name }}</span>
              </el-form-item>
              <el-form-item label="型号">
                <span>{{ props.row.model_name.model_name }}</span>
              </el-form-item>
              <el-form-item label="序列号">
                <span>{{ props.row.sn }}</span>
              </el-form-item>
              <el-form-item label="UUID">
                <span>{{ props.row.uuid }}</span>
              </el-form-item>
              <el-form-item label="Last Check">
                <span>{{ props.row.last_check }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.ctime }}</span>
              </el-form-item>
              <el-form-item label="网卡">
                <span v-for="i in props.row.device ">
                  <span v-for="ip in i.ips">
                    - <strong>{{ i.name }}</strong> [{{ i.mac }}] - {{ ip.ip }} <br>
                  </span>
                </span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>

        <!-- 表格正常显示列 -->
        <!--<el-table-column align="center" prop="id" label="ID"></el-table-column>-->
        <el-table-column width="120" sortable align="center" prop="hostname" label="主机名"></el-table-column>
        <el-table-column width="120" sortable align="center" prop="ip" label="IP"></el-table-column>
        <el-table-column width="120" sortable align="center" prop="os" label="操作系统"></el-table-column>
        <el-table-column width="120" sortable align="center" prop="business_parent.name" label="业务线"></el-table-column>
        <el-table-column width="120" sortable align="center" prop="business_line.name" label="产品线"></el-table-column>
        <el-table-column width="120" sortable align="center" prop="idc.name" label="所属机房"></el-table-column>
        <el-table-column sortable align="center" prop="status" label="状态"></el-table-column>
        <el-table-column sortable align="center" prop="last_check" label="Last Check" width="160"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="120">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pageination" v-show="total>5">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[5, 10, 15, 20]"
          :current-page.sync="params.page"
          :page-size="params.page_size"
          :total="total">
        </el-pagination>
      </div>

      <!-- 修改服务器表单 -->
      <el-dialog title="修改服务器" :visible.sync="changeServerVisible" width="40%">
        <el-form ref="changeServerForm" :model="changeServerForm" label-width="70px" :rules="changeServerFormRules">
          <el-form-item label="主机名称" prop="hostname">
            <el-input v-model="changeServerForm.hostname" :disabled="true" placeholder="请输入主机名称"></el-input>
          </el-form-item>
          <el-form-item label="管理IP" prop="ip">
            <el-input v-model="changeServerForm.ip" :disabled="true" placeholder="请输入IP地址"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select class="select" v-model="changeServerForm.status">
              <el-option v-for="(item, index) in statusList" :key="index"
                         :label="item" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机房" prop="idc">
            <el-select class="select" v-model="changeServerForm.idc" @change="changeIdc">
              <el-option v-for="(item, index) in idcsList" :key="index"
                         :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机柜" prop="cabinet">
            <el-select class="select" v-model="changeServerForm.cabinet">
              <el-option v-for="(item, index) in cabinetList" :key="index"
                         :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务线" prop="business_parent">
            <el-select class="select" v-model="changeServerForm.business_parent" @change="changeBusiness">
              <el-option v-for="(item, index) in businessParentList" :key="index"
                         :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="产品线" prop="business_line">
            <el-select class="select" v-model="changeServerForm.business_line">
              <el-option v-for="(item, index) in businessLineList" :key="index"
                         :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="changeServerForm.remark" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="cancleFormClick">取 消</el-button>
          <el-button size="small" type="primary" @click="submitChangeServerClick">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { getServersList, updateServer, deleteServer, getBusinessList, getIdcsList, getCabinetsList } from '@/api/resources'

  export default {
    name: 'server',
    data() {
      return {
        isLoading: false,
        state: 0,
        serverList: [],
        params: {
          page: 1,
          page_size: 10
        },
        total: 0,
        businessParentList: [],
        businessLineList: [],
        idcsList: [],
        cabinetList: [],
        // statusList: [
        //   { id: 0, name: '线上' },
        //   { id: 1, name: '下线' },
        //   { id: 2, name: '故障' },
        //   { id: 3, name: '测试' },
        //   { id: 4, name: '发布' },
        //   { id: 5, name: '维护' }
        // ],
        statusList: [ // 所有状态
          '线上',
          '下线',
          '故障',
          '测试',
          '发布',
          '维护'
        ],
        searchCabinetList: [{ id: '', name: '所有机柜' }],
        searchBusinessLineList: [{ id: '', name: '所有产品线' }],
        searchForm: { // 组合搜索参数
          hostname: '',
          idc: '',
          cabinet: '',
          business_parent: '',
          business_line: '',
          page: 1
        },
        // 表单
        // formCabinet: '',
        changeServerForm: {
          id: '',
          hostname: '',
          ip: '',
          status: '',
          remark: '',
          idc: '',
          cabinet: '',
          business_parent: '',
          business_line: ''
        },
        changeServerFormRules: {
          status: [
            { required: true, trigger: 'blur', message: '请选择状态' }
          ],
          idc: [
            { required: true, trigger: 'blur', message: '请选择机房' }
          ],
          cabinet: [
            { required: true, trigger: 'blur', message: '请选择机柜' }
          ],
          business_parent: [
            { required: true, trigger: 'change', message: '请选择业务线' }
          ],
          business_line: [
            { required: true, trigger: 'change', message: '请选择产品线' }
          ]
        },
        changeServerVisible: false
      }
    },
    computed: {
      searchIdcsList() {
        return [{ id: '', name: '所有机房' }].concat(this.idcsList)
      },
      searchBusinessList() {
        return [{ id: '', name: '所有业务线' }].concat(this.businessParentList)
      }
    },
    watch: {
      state() {
        getIdcsList({ page_size: 0 }).then(
          res => {
            this.idcsList = res.map(item => {
              return { id: item.id, name: item.name }
            })
          })
        getBusinessList({ pid: 0, page_size: 0 }).then(
          res => {
            this.businessParentList = res.map(
              item => {
                return { id: item.id, name: item.name }
              })
          })
        this.fetchData()
        // getNetworkDeviceList({ page_size: 0 }).then(res => {
        //   this.cardList = res
        //   console.log(this.cardList)
        // })
        // getIpList({ page_size: 0 }).then(res => {
        //   this.ipList = res
        // })
        // getCabinetList({ page_size: 0 }).then(res => {
        //   this.cabinetList = res
        //   console.log(this.cabinetList)
        // })
      },
      'searchForm.idc'(val) {
        this.searchCabinetList = [{ id: '', name: '所有机柜' }]
        this.searchForm.cabinet = ''
        getCabinetsList({ idc: val, page_size: 0 }).then(
          res => {
            this.searchCabinetList = this.searchCabinetList.concat(res.map(item => {
              return { id: item.id, name: item.name }
            }))
          })
      },
      'searchForm.business_parent'(val) {
        this.searchBusinessLineList = [{ id: '', name: '所有产品线' }]
        this.searchForm.business_line = ''
        getBusinessList({ pid: val, page_size: 0 }).then(res => {
          console.log(res)
          this.searchBusinessLineList = this.searchBusinessLineList.concat(res.map(item => {
            return { id: item.id, name: item.name }
          }))
        })
      },
      'changeServerForm.business_parent'(val) {
        if (!val) {
          this.changeServerForm.business_line = ''
          return
        }
        getBusinessList({ pid: val, page_size: 0 }).then(res => {
          this.businessLineList = res.map(
            item => {
              return { id: item.id, name: item.name }
            })
        })
      },
      'changeServerForm.idc'(val) {
        if (!val) {
          this.changeServerForm.cabinet = ''
          return
        }
        getCabinetsList({ idc: val, page_size: 0 }).then(
          res => {
            this.cabinetList = res.map(item => {
              return { id: item.id, name: item.name }
            })
          })
      }
    },
    created() {
      this.state = 1
    },
    methods: {
      fetchData(params) {
        this.isLoading = true
        getServersList(params).then(
          res => {
            this.serverList = res.results
            this.total = res.count
            this.isLoading = false
          }
        )
      },
      // 改变页码和页面大小时获取对应的数据
      handleCurrentChange(val) {
        this.params.page = val
        this.fetchData()
      },
      handleSizeChange(val) {
        this.params.page_size = val
        this.fetchData(Object.assign(this.searchForm))
      },
      searchClick() {
        // console.log(this.searchForm)
        this.fetchData(Object.assign(this.searchForm))
      },
      handleEdit(row) {
        if (this.$refs['changeServerForm'] !== undefined) {
          this.$refs['changeServerForm'].resetFields()
        }
        const { id, hostname, ip, status, remark } = row
        const { idc, business_parent, business_line, cabinet } = { idc: row.idc.id, business_parent: row.business_parent.id,
          business_line: row.business_line.id, cabinet: row.cabinet.id }
        this.changeServerForm = { id, hostname, ip, status, remark, idc, cabinet, business_parent, business_line }
        this.changeServerVisible = true
      },
      handleDelete(row) {
        this.$confirm('是否删除 ' + row.hostname + '【' + row.ip + '】?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(
          () => {
            deleteServer(row.id).then(
              () => {
                this.$message.success('删除服务器成功')
                if (this.serverList.length === 1 && this.searchForm.page > 1) {
                  this.searchForm.page = this.searchForm.page - 1
                }
                this.fetchData(Object.assign(this.searchForm))
              })
            // this.$message.error('不允许手动删除服务器')
          }).catch(() => {
          this.$message.error('不允许手动删除服务器')
        })
      },
      changeIdc() {
        this.changeServerForm.cabinet = ''
      },
      changeBusiness() {
        this.changeServerForm.business_line = ''
      },
      submitChangeServerClick() {
        this.$refs['changeServerForm'].validate((valid) => {
          if (!valid) {
            return
          }
          updateServer(this.changeServerForm.id, this.changeServerForm).then(
            () => {
              this.changeServerVisible = false
              this.fetchData(Object.assign(this.searchForm))
              this.$message.success('操作成功')
              this.$refs['changeServerForm'].resetFields()
            }).catch(
            () => {
              this.changeServerVisible = false
              this.$message.error('修改失败')
            }
          )
        })
      },
      cancleFormClick() {
        this.changeServerVisible = false
        this.$refs['changeServerForm'].resetFields()
      }
    }
  }
</script>


<style scoped>

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

  .pageination {
    text-align: center;
    margin-top: 15px;
  }

  .select {
    width: 100%;
  }
  .top-form .el-form-item {
    margin-right: 5px;
    margin-bottom: 10px;
    /*width: 50%;*/
  }
</style>
